package com.tanhua.manage.controller;

import cn.hutool.core.convert.Convert;
import com.tanhua.common.pojo.UserFreeze;
import com.tanhua.manage.service.ManageService;
import com.tanhua.manage.vo.MessagesVo;
import com.tanhua.manage.vo.PageResult;
import com.tanhua.manage.vo.UsersInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author Ling
 * @Title: null.java
 * @Package com.tanhua.manage.controller
 * @Description:
 * @date 2021/9/11 21:28
 */
@RestController
@RequestMapping("manage")
public class ManageController {
    @Autowired
    private ManageService service;

    @GetMapping("videos")
    public ResponseEntity<PageResult> getVideos(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                                @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize,
                                                @RequestParam(value = "sortProp") String sortProp,
                                                @RequestParam(value = "sortOrder") String sortOrder,
                                                @RequestParam(value = "uid") Long userId) {
//TODO 尝试调用videoservice
        try {
            if (page <= 0) {
                page = 1;
            }
            PageResult pageResult = this.service.getVideosListByUserId(userId, page, pageSize, sortProp, sortOrder);
            if (null != pageResult) {
                return ResponseEntity.ok(pageResult);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();

    }

    @GetMapping("logs")
    public ResponseEntity<PageResult> getLoginLogs(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                                   @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize,
                                                   @RequestParam(value = "sortProp", required = false) String sortProp,
                                                   @RequestParam(value = "sortOrder", required = false) String sortOrder,
                                                   @RequestParam(value = "type", required = false) String type,
                                                   @RequestParam(value = "uid") Long userId) {

        try {
            if (page <= 0) {
                page = 1;
            }
            PageResult pageResult = this.service.getLoginLogs(userId, page, pageSize, sortProp, sortOrder);
            if (null != pageResult) {
                return ResponseEntity.ok(pageResult);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();

    }

    @PostMapping("messages/{id}/top")
    public ResponseEntity<Map<String, String>> topMessages(@PathVariable("id") String id) {
        Map<String, String> m = service.topMessages(id);
        if (null != m) {
            return ResponseEntity.ok(m);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    @PostMapping("messages/{id}/untop")
    public ResponseEntity<Map<String, String>> untopMessages(@PathVariable("id") String id) {
        Map<String, String> m = service.untopMessages(id);
        if (null != m) {
            return ResponseEntity.ok(m);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    @GetMapping("messages/{id}")
    public ResponseEntity<MessagesVo> getMessage(@PathVariable("id") String id) {
        MessagesVo messagesVo = service.getMessage(id);
        if (null != messagesVo) {
            return ResponseEntity.ok(messagesVo);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    //    /manage/messages/:id/top


    //    GET/manage/messages/comments


    @GetMapping("messages/comments")
    public ResponseEntity<PageResult> getComments(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                                  @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize,
                                                  @RequestParam(value = "sortProp") String sortProp,
                                                  @RequestParam(value = "sortOrder") String sortOrder,
                                                  @RequestParam(value = "messageID") String messageID) {

        try {
            if (page <= 0) {
                page = 1;
            }
            PageResult pageResult = this.service.getComments(messageID, page, pageSize, sortProp, sortOrder);
            if (null != pageResult) {
                return ResponseEntity.ok(pageResult);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();

    }
    @GetMapping("messages")
    public ResponseEntity<PageResult> getMessages(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                                  @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize,
                                                  @RequestParam(value = "sortProp") String sortProp,
                                                  @RequestParam(value = "sortOrder") String sortOrder,
                                                  @RequestParam(value = "uid") Long  uid) {
        try {
            if (page <= 0) {
                page = 1;
            }
            PageResult pageResult = this.service.getMessages(uid, page, pageSize, sortProp, sortOrder);
            if (null != pageResult) {
                return ResponseEntity.ok(pageResult);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();

    }

    @PostMapping("users/freeze")
    public ResponseEntity<Map<String, String>> freezeUser(/*@RequestParam(value = "userId") Long userId,
                                                          @RequestParam(value = "freezingTime") Integer freezingTime,
                                                          @RequestParam(value = "freezingRange") Integer freezingRange,
                                                          @RequestParam(value = "reasonsForFreezing") String reasonsForFreezing,
                                                          @RequestParam(value = "frozenRemarks") String frozenRemarks*/
            @RequestBody UserFreeze freeze) {

        Map<String, String> m = service.freezeUser(freeze);
        if (null != m) {
            return ResponseEntity.ok(m);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();

    }

    @PostMapping("users/unfreeze")
    public ResponseEntity<Map<String, String>> unfreezeUser(/*@RequestParam(value = "userId") Long userId,
                                                            @RequestParam(value = "reasonsForThawing") String reasonsForThawing*/
            @RequestBody Map<String, String> map) {

        try {
            Long userId = Convert.toLong( map.get("userId"));
            String reasonsForThawing = map.get("reasonsForThawing");
            Map<String, String> m = service.unfreezeUser(userId, reasonsForThawing);
            if (null != m) {
                return ResponseEntity.ok(m);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();

    }

    @GetMapping("users/{id}")
    public ResponseEntity<UsersInfoVo> getUsersInfo(@PathVariable("id") Long id) {

        try {

            UsersInfoVo usersInfo = this.service.getUsersInfo(id);
            if (null != usersInfo) {
                return ResponseEntity.ok(usersInfo);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    @GetMapping("users")
    public ResponseEntity<PageResult> getUsersList(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                                   @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize,
                                                   @RequestParam(value = "id", required = false) Integer id,
                                                   @RequestParam(value = "nickname", required = false) String nickname,
                                                   @RequestParam(value = "city", required = false) String city) {
        try {
            PageResult pageResult = this.service.getUsersList(page, pageSize, id, nickname, city);
            if (null != pageResult) {
                return ResponseEntity.ok(pageResult);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }


}
